<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>学生注册</title>
<link rel="stylesheet" type="text/css" href="CSS/bootstrap.css">
<script type="text/javascript" src="JS/bootstrap.min.js"></script>
<script type="text/javascript" src="JS/jquery.js"></script>
<style>
    body{
        background: url(img/1.jpg);
        font-family: "microsoft yahei";
        min-width: 800px;
    }
    img{
        width: 100%;
        height: auto;
    }
</style>

<div>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <button class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-title">
                 <h1 class="text-center">学生注册</h1>
            </div>
            <div class="modal-body">
                <form class="form-group" action="" method="post">
                    <div class="form-group">
                        <label for="">昵称</label>
                        <input class="form-control" type="text" placeholder="不超过五个字" name="stu_name">
                    </div>
                    <div class="form-group">
                         <label for="">账号</label>
                        <input class="form-control" type="text" placeholder="6-15位字母或数字" name="stu_number">
                    </div>
                    <div class="form-group">
                        <label for="">密码</label>
                        <input class="form-control" type="password" placeholder="至少6位字母或数字" name="passwd">
                    </div>
                    <div class="form-group">
                        <label for="">确认密码</label>
                        <input class="form-control" type="password" placeholder="至少6位字母或数字" name="again_passwd">
                    </div>
                    <div class="text-right">
                        <input class="btn btn-primary" type="submit" name="submit" value="提交">
                        <input class="btn btn-danger" data-dismiss="modal" type="submit" name="quxiao" value="取消">
                    </div>
                     <a href="index.php" data-toggle="modal" data-dismiss="modal">已有账号？点我登录</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php
if(isset($_POST["submit"]) && $_POST["submit"] == "提交") {

    date_default_timezone_set("Asia/Shanghai");
    $insertdate = date("Y-m-d H:m:s");
    $stu_number = $_POST["stu_number"];
    $passwd = md5($_POST["passwd"]);
    $again_passwd = md5($_POST["again_passwd"]);
    $stu_name = $_POST["stu_name"];

    $selectstusql = "select * from students where stu_name='$stu_name'";

    include_once("database.php");

    $result = @mysql_query($selectstusql) or die("查询同名用户的时候出现错误!");
    
    $result = mysql_num_rows($result);

    if($result > 0) {
        //存在同名记录
        echo "<font color='red'>昵称<span style='color:blue;font-size: 20px'>{$stu_name}</span>已经被注册,请重新填写用户名!</font>";
    } else {

        if($passwd == $again_passwd) {

            $insertstusql = "insert into students(stu_number,stu_password,stu_name,stu_insertdate) 
            values('$stu_number','$passwd','$stu_name','$insertdate')";
            
            include_once("database.php");

            @mysql_query($insertstusql) or die("用户注册失败:".mysql_error()."<br/>");

            header("Location:index.php");

        } else {
            echo "<script>alert('两次密码不一致！');</script>";
        }
    }
}

if(isset($_POST["quxiao"]) && $_POST["quxiao"] == "取消") {
    header("Location:index.php");
}
?>
